package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"registration-system/models/mysql"
)

type RegistrationController struct {
	beego.Controller
}

func (c *RegistrationController) Add() {
	name := c.GetString("name")
	mobile := c.GetString("mobile")
	registrationNumber, _ := c.GetInt64("registrationNumber")
	price, _ := c.GetFloat("price")
	registrationTime := c.GetString("registrationTime")

	var register mysql.Registration

	register.Name = name
	register.Mobile = mobile
	register.RegistrationNumber = registrationNumber
	register.Price = price
	register.RegistrationTime = registrationTime

	err := register.RegistrationAdd()

	if err != nil {
		c.Data["json"] = map[string]interface{}{
			"msg":  "挂号失败",
			"code": 500,
		}
		c.ServeJSON()
		return
	}

	c.Data["json"] = map[string]interface{}{
		"msg":  "挂号成功",
		"code": 200,
	}
	c.ServeJSON()
	return
}

func (c *RegistrationController) List() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	var register mysql.Registration
	registration, err := register.GetList()
	if err != nil {
		c.Data["json"] = map[string]interface{}{
			"msg":  "展示失败",
			"code": 500,
		}
		c.ServeJSON()
		return
	}

	c.Data["json"] = map[string]interface{}{
		"msg":  "展示成功",
		"code": 200,
		"data": registration,
	}
	c.ServeJSON()
	return
}
